'
'   XC Engine. A simple engine to drive the Internet Explorer web browser
'   Copyright (C) 2009 Stanislav Hordiyenko
'
'   This program is free software; you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation; either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program; if not, see http://www.gnu.org/licenses
'   or write to the Free Software Foundation,Inc., 51 Franklin Street,
'   Fifth Floor, Boston, MA 02110-1301 USA
'

Class IE
	Private objIE
	
	Public Sub Class_Initialize()
		Set objIE = Nothing
	End Sub
	
	Public Sub Class_Terminate()
		Set objIE = Nothing
	End Sub
	
	Public Sub WrapBrowser(ByVal objIEBrowser)
		If Not(objIEBrowser Is Nothing) Then
			If UCase(TypeName(objIEBrowser)) = "IWEBBROWSER2" Then
				Set objIE = objIEBrowser
					
				Exit Sub
			End If
		End If

		Err.Raise 10001, "IE->WrapBrowser", "The Browser couldn't be wrapped"
	End Sub
	
	Public Function Exists()
		On Error Resume Next
		
		Exists = False

		strHtmlElementTagName = objIE.Name
		
		If Err.Number = 0 Then
			Exists = True
			
			Exit Function
		End If
		
		Err.Clear
		
		On Error Goto 0
	End Function
	
	Public Sub Navigate(ByVal strUrl)
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
	
		objIE.Navigate strUrl

		Call Synchronize()
	End Sub
	
	Private Sub Synchronize()
		Do Until objIE.ReadyState = 4
			WScript.Sleep 10
		Loop

		Do Until objIE.Document.ReadyState = "complete"
			WScript.Sleep 10
		Loop

		Call Document().Synchronize()
	End Sub
	
	Public Function Object()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Function
		End If
	
		Set Object = objIE
	End Function
	
	Public Sub Close()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
		
		objIE.Quit
	End Sub
	
	Public Sub SetCustomProperty(ByVal strPropertyName, ByVal strPropertyValue)
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
	
		objIE.PutProperty strPropertyName, strPropertyValue
	End Sub
	
	Public Function GetCustomProperty(ByVal strPropertyName)
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Function
		End If
		
		GetCustomProperty = objIE.GetProperty(strPropertyName)
	End Function

	Public Sub Home()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
	
		objIE.GoHome
		
		Call Synchronize()
	End Sub
	
	Public Sub Back()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
	
		objIE.GoBack
		
		Call Synchronize()
	End Sub
	
	Public Sub Forward()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
		
		objIE.GoForward
		
		Call Synchronize()
	End Sub
	
	Public Sub Refresh()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
		
		objIE.Refresh2
		
		Call Synchronize()
	End Sub
	
	Public Sub StopRefresh()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Sub
		End If
		
		objIE.Stop
		
		Call Synchronize()
	End Sub
	
	Public Function Document()
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Function
		End If
	
		Set objDocument = new Page

		Call objDocument.WrapElement(Me)

		Set Document = objDocument
	End Function
	
	Public Function GetProperty(ByVal strPropertyName)
		If Not Exists() Then
			Err.Raise 10002,  "IE->Navigate", "The IE doesn't exist"
			
			Exit Function
		End If
	
		strPropertyName = LCase(strPropertyName)
		
		Select Case strPropertyName
			Case "hwnd"
				GetProperty = objIE.Hwnd
		
			Case "url"
				GetProperty = objIE.LocationURL
			
			Case "left"
				GetProperty = objIE.Left
				
			Case "top"
				GetProperty = objIE.Top
			
			Case "height"
				GetProperty = objIE.Height
			
			Case "width"
				GetProperty = objIE.Width
			
			Case "language"
				GetProperty = objIE.Document.Script.Navigator.BrowserLanguage
			
		End Select
	End Function
End Class